package com.zyb.system.shop.controller;

import com.zyb.constans.BaseConstants;
import com.zyb.system.shop.domain.ShopAuditLog;
import com.zyb.system.shop.service.IShopAuditLogService;
import com.zyb.system.shop.service.IShopService;
import com.zyb.system.shop.domain.Shop;
import com.zyb.system.shop.query.ShopQuery;
import com.zyb.utils.AjaxResult;
import com.zyb.utils.BusinessException;
import com.zyb.utils.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;
    @Autowired
    private IShopAuditLogService shopAuditLogService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.add(shop);
            return AjaxResult.success().setMsg("保存成功");
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg("保存失败！"+e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg("网络异常,请联系管理员");
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error().setMsg("删除失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop queryById(@PathVariable("id")Long id)
    {
        return shopService.queryById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping("/list")
    public List<Shop> queryAll(){
        return shopService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/pageList")
    public AjaxResult pageList(@RequestBody ShopQuery query){
        try {
            List<Shop> page = shopService.queryPage(query);
            Long count = shopService.queryCount(query);
            return AjaxResult.success().setData(page).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
            return AjaxResult.error().setMsg("获取分页数据失败");
    }


    @PostMapping("/audit/pass")
    public AjaxResult auditPass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            //设置商店审核状态
            shopAuditLog.setState(BaseConstants.Shop.STATE_WAIT_ACTIVE);
            shopAuditLogService.add(shopAuditLog);
            return AjaxResult.success().setMsg("审批成功");
        }catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg("审核数据失败,请稍后再试");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg("网络异常,请稍后再试");
        }
    }

    @PostMapping("/audit/reject")
    public AjaxResult auditReject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            //设置商店审核状态
            shopAuditLog.setState(BaseConstants.Shop.STATE_REJECT_AUDIT);
            shopAuditLogService.add(shopAuditLog);
            return AjaxResult.success().setMsg("审批成功");
        }catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg("审核数据失败,请稍后再试");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg("网络异常,请稍后再试");
        }
    }

    @GetMapping("/export")
    public void Export(HttpServletResponse response){
        try {
            //查询全部数据
            List<Shop> shops = shopService.queryAll();
            //使用工具类排版数据
            ExcelUtil.exportExcel(shops,"商铺详细信息","shop数据",Shop.class,"商店的详细信息.xls",response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @PostMapping("/importExcel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        List<Shop> shops = ExcelUtil.importExcel(file, 1, 1, Shop.class);
        shops.forEach(System.out::println);
    }

    @GetMapping("/activation/{shopId}")
    public String activation(@PathVariable("shopId")Long shopId){
        shopService.activation(shopId);
        return "激活成功！";
    }
}
